package GUI;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;

import ctrLayer.ForestOwnerCtr;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

/**
 * OwnerPanel
 * @author Thomas
 *
 */
public class OwnerPanel extends JPanel {
	
	private JTable ownerTable;
	private JScrollPane scrollPane;
	
	private ForestOwnerCtr forestOwnerCtr;
	private static OwnerPanel instance;
	
	
	private OwnerPanel() {
		forestOwnerCtr = ForestOwnerCtr.getInstance();
		
		setLayout(null);
		
		initilizeOwnerTable();
		intilizaComponents();
		
	}
	
	public static OwnerPanel getInstance() {
		if(instance == null) {
			instance = new OwnerPanel();
		}
		return instance;
	}
	
	/**
	 * Method that creates the owner table
	 */
	private void initilizeOwnerTable() {
		String[][] data = forestOwnerCtr.ownerListToArray();
		String[] columns = {"Owner nr", "Owner name", "Owner address", "Owner phonenumber"};
	
		ownerTable = new JTable(data, columns) {
			private static final long serialVersionUID = 1L;
			
			public boolean isCellEditable(int row, int column) {
				return false;
			}
		};
		
		ownerTable.getTableHeader().setReorderingAllowed(false);
		ownerTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		scrollPane = new JScrollPane(ownerTable);
		ownerTable.setFillsViewportHeight(true);
		ownerTable.setAutoCreateRowSorter(true);
		scrollPane.setBounds(40,40,685,410);
		add(scrollPane);
		
		ownerTable.setVisible(true); 
		}
		
	private void intilizaComponents() {
		JButton btnCreate = new JButton("Create Owner");
		btnCreate.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				new CreateOwnerFrame();
			}
		});
		btnCreate.setBounds(611, 461, 114, 23);
		add(btnCreate);
	}
	
	public void updatePanel() {
		ownerTable.removeAll();
		this.remove(ownerTable);
		scrollPane.removeAll();
		this.remove(scrollPane);
		initilizeOwnerTable();
	}
		


}
